import os
import shutil
try:
    from .cvio import cvio
    from .running_faster import Runner
    from .sample_scls_data import parse_text
except Exception as e:
    print(e)
    from cvio import cvio
    from running_faster import Runner
    from sample_scls_data import parse_text

def copyAllSubFolderImages(src, dst, ignore='', worker='thread', num_workers=1):
    ignores = parse_text(ignore)
    images = cvio.load_image_list(src, recursive=True, silent=False)

    n = len(images)
    if n < 1:
        return

    if not os.path.exists(dst):
        os.makedirs(dst)

    new_images = []
    for i, img in enumerate(images, 1):
        flag = False
        for ignore in ignores:
            if ignore in img:
                flag = True
                break
        if flag:
            continue
            
        if os.path.exists(os.path.join(dst, os.path.basename(img))):
            continue
        new_images.append(img)

    n = len(new_images)
    if n < 1:
        return

    runner = Runner()
    for i, img in enumerate(new_images, 1):
        runner.append(copy_one_item, (i, n, img, dst))

    runner.run(mode=worker, num_workers=num_workers)

def copy_one_item(i, n, src, dst):
    print('[%d/%d] %s' % (i, n, src))
    shutil.copy(src, dst)
    ann = os.path.splitext(src)[0] + '.json'
    if os.path.exists(ann):
        shutil.copy(ann, dst)

if __name__ == '__main__':
    src = r'G:\data\datasets\drink\daiding_101\base_train'
    dst = r'G:\data\datasets\drink\daiding_101\train\JPEGImages'
    shutil.rmtree(dst)
    ignores = ['dontwant2see', 'ann4cls', 'ann1cls', '_new_add', 'aiot_bottle_bkl_train_A_notok']
    copyAllSubFolderImages(src, dst, ignore=ignores , worker='thread', num_workers=16)